/**
 * @file dhcp_wizi.h
 * @brief Defines DHCP related functions, specific to the CC2530 chip in 
 *        the wizi project.
 * @author Vamshi Ponnapalli
 */

#ifndef _DHCP_WIZI_H
  #define _DHCP_WIZI_H
  /*****************************************************************************
   * Includes
   ****************************************************************************/
  #include "hal_types.h"
  #include "hal_defs.h"
  #include "network.h"

  #ifdef __cplusplus
    extern "C" {
  #endif

  /*****************************************************************************
   * Function declarations
   ****************************************************************************/
  /**
   * Updates the input message with parameters that create a basic DHCP message
   * without any specific options.
   *
   * @param pMsg A pointer to a message object that will be updated by this 
   *            function
   * @param macAddress The MAC address of the client that will be sending out 
   *                   this message.
   * @param sessionId The DHCP session id of the session that will be using
   *                  this message.
   */
  void dhcp_buildBasicMessage(dhcp_message_t *pMsg,
                                 mac_address_t macAddress,
                                 uint32 sessionId);
  
  /**
   * Updates the input message with options that make the message represent
   * a DHCP discover message.
   *
   * @param pMsg A pointer to a message object that will be updated by this 
   *            function
   * 
   * @return The size of the message (including options)
   */
  //TODO: Refactor this.
  uint16 dhcp_buildDiscoverMessage(dhcp_message_t *pMsg,
                                 mac_address_t macAddress,
                                 uint32 sessionId);
  
  /**
   * Updates the input message with options that make the message represent a
   * DHCP request message.
   * 
   * @param pMsg A pointer to a message object that will be updated by this 
   *            function
   * @param ipAddress The ipAddress originally offered by the server.
   * @param serverId The id of the server.
   *
   * @return The size of the message (including options)
   */
  //TODO: Refactor this.
  uint16 dhcp_buildRequestMessage(dhcp_message_t *pMsg,
                                 mac_address_t macAddress,
                                 uint32 sessionId,
                                 ip_address_t ipAddress,
                                 uint32 serverId);

  /**
   * Locates and returns a specified option within a DHCP message.
   *
   * @param pMsg Reference to a DHCP message that needs to be processed.
   * @param messageSize The size of the DHCP message (including header).
   * @param option The option to find.  
   * 
   * return A structure that contains information regarding the option value
   *        (location, size) within the message.
   */
  dhcp_option_value_t dhcp_getOption(dhcp_message_t *pMsg, uint16 messageSize, 
                                                           uint8 option);
  
  /**
   * Validates a DHCP message sent by the server.
   *
   * @param pMsg Reference to the DHCP message that needs to be validated.
   * @param messageSize The size of the DHCP message (including header).
   * @param sessionId The session id of the DHCP protocol session.
   * @param pServerId A reference to the server id of the server if known. If 
   *                  this value is not known, the call will populate this 
   *                  refernce based on the message from the server.
   *
   * @return SUCCESS if the message was successfully validated, FAILED 
   *         otherwise.
   */
  uint8 dhcp_validateServerMessage(dhcp_message_t *pMsg, uint16 messageSize,
                                   uint32 sessionId, uint32 *pServerId);
  
  #ifdef __cplusplus
    }
  #endif

#endif
